package edu.gyc.hiblog.controller;

import com.github.pagehelper.PageInfo;
import edu.gyc.hiblog.service.CommentService;
import edu.gyc.hiblog.service.PostService;
import edu.gyc.hiblog.vo.CommentVo;
import edu.gyc.hiblog.vo.PostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class CategoryController extends BaseController{

    @Autowired
    private PostService postService;

    @Autowired
    private CommentService commentService;

    @GetMapping("/category/{id:\\d*}")
    public String category(@PathVariable(name = "id") Long id,
                           @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        req.setAttribute("currentCategoryId",id);
        req.setAttribute("pageNum",pageNum);
        return "fenlei/category";
    }

    @GetMapping("/post/{id:\\d*}")
    public String detail(@PathVariable(name = "id") Long id,
                         @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {

        PostVo postVo=postService.selectOnePostVo(id);
        postService.putViewCount(postVo);

        PageInfo<CommentVo> commentVoPageInfo = commentService.paging(pageNum, postVo.getId(), null);

        req.setAttribute("currentCategoryId",postVo.getCategoryId());
        req.setAttribute("post",postVo);
        req.setAttribute("pageData",commentVoPageInfo);

        return "fenlei/detail";
    }
}
